package com.shensi.appCollect.service.impl;

import com.shensi.appCollect.dao.OilEngineMapper;
import com.shensi.appCollect.pojo.Enterprise;
import com.shensi.appCollect.pojo.OilEngine;
import com.shensi.appCollect.service.EnterpriseService;
import com.shensi.appCollect.service.OilEngineService;
import com.shensi.appCollect.utils.DateUtils;
import com.shensi.appCollect.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@Service("oilEngineService")
@Slf4j
public class OilEngineServiceImpl implements OilEngineService {

    @Resource
    private OilEngineMapper oilEngineMapper;

    @Autowired
    private EnterpriseService enterpriseService;

    @Override
    public int saveOilEngine(OilEngine oilEngine) {
        if(Objects.isNull(oilEngine)){
            throw new CustomException("油机数据录入信息参数为空");
        }
        if(Objects.nonNull(oilEngine.getEnterpriseId())) {
            Enterprise enterprise = enterpriseService.selectEnterpriseInfo(oilEngine.getEnterpriseId().intValue());
            if(Objects.nonNull(enterprise)){
                oilEngine.setEnterpriseName(enterprise.getEnterpriseName());
            }
        }

        int i = oilEngineMapper.insertSelective(oilEngine);
        return i;
    }

    @Override
    public List<OilEngine> selectOilEngineList(OilEngine oilEngine) {
        // 获取10分钟内的订单
        oilEngine.setBeginTime(DateUtils.beforeThirtyMinutes(10));
        return oilEngineMapper.selectOilEngineList(oilEngine);
    }

    @Override
    public int updateOilEngine(OilEngine oilEngine) {
        if(Objects.isNull(oilEngine) || Objects.isNull(oilEngine.getId())){
            throw new CustomException("修改油机订单失败");
        }
        return oilEngineMapper.updateByPrimaryKeySelective(oilEngine);
    }
}
